package cn.bone.motan.demo1.config;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by MD-01 on 2017/4/18.
 */
@Configuration
public class MongoConfig {

    @Bean
    public MongoClient mongoClient(){

        String uri = "mongodb://dcw:superbone@118.178.225.7:17017/dcw";
        String uri2 = "mongodb://127.0.0.1/dcw";
        MongoClientURI mongoClientURI = new MongoClientURI(uri2);
        MongoClient client = new MongoClient(mongoClientURI);
        return client;
    }

    @Bean
    public Datastore datastore(){
        Morphia morphia = new Morphia();
        morphia.mapPackage("cn.bone.**");
        //如果要使实体类上的index注解有效,这里要手动使用
        //morphia.mapPackageFromClass(Xxx.class)

        MongoClient client = mongoClient();
        Datastore datastore = morphia.createDatastore(client,"dcw");
        return datastore;
    }
}
